/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.wicket;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;

/**
 * 
 * @author servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class WicketDateTextFieldEventBehavior extends AjaxFormComponentUpdatingBehavior{

	private static final long serialVersionUID = 1L;
	private WicketDateChooser parent;
	private boolean disableFocusOnBlur = true;
	
	public final static String KEY_DOWN = "onkeydown";
	public final static String DATA_CHANGE = "onchange";
	public final static String FOCUS_GAINED = "onfocus";
	public final static String FOCUS_LOST = "onblur";
	
	public WicketDateTextFieldEventBehavior(String event, WicketDateChooser parent) {
		super(event);
		this.parent = parent;
	}
	
	public WicketDateTextFieldEventBehavior(String event) {
		super(event);
	}
	
	/* (non-Javadoc)
	 * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#disableFocusOnBlur()
	 */
	@Override
	protected boolean disableFocusOnBlur() {
		return disableFocusOnBlur;
	}
		
	/* (non-Javadoc)
	 * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getAjaxCallDecorator()
	 */
	@Override
	protected IAjaxCallDecorator getAjaxCallDecorator() {
		if (getEvent().equalsIgnoreCase(KEY_DOWN)) {
			return new AjaxCallDecorator() {
				private static final long serialVersionUID = 1L;
				
				public CharSequence decorateScript(CharSequence script) {
					return "return testEnterKey(event, function() {" + script + "});";
				}
			};
		}
		return null;
	}


	/* (non-Javadoc)
	 * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#onUpdate(org.apache.wicket.ajax.AjaxRequestTarget)
	 */
	@Override
	protected void onUpdate(AjaxRequestTarget target) {
		target.addComponent(parent);
		disableFocusOnBlur = true;
		if (parent.getValidationEnabled()) {
			String evt = getEvent();
			if (evt.equalsIgnoreCase(FOCUS_GAINED)) {
				parent.fireFocusGainedCallBack();
				disableFocusOnBlur = false;
			} else if (evt.equalsIgnoreCase(FOCUS_LOST)) {
				if (parent.testChanged()) {
					parent.setValid();
					parent.fireFocusLostCallBack();
				}
			} else if (evt.equalsIgnoreCase(DATA_CHANGE)) {
				if (parent.testChanged()) {
					parent.resetForeground();
					parent.fireDataChangeCallBack(null);
				}
			} else if (evt.equalsIgnoreCase(KEY_DOWN)) {
				if (parent.testChanged()) {
					parent.resetForeground();
					parent.fireDataChangeCallBack(null);
					parent.fireActionCallBack();
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#onError(org.apache.wicket.ajax.AjaxRequestTarget, java.lang.RuntimeException)
	 */
	@Override
	protected void onError(AjaxRequestTarget target, RuntimeException e) {
		super.onError(target, e);
		if (parent.getValidationEnabled()) {
			target.focusComponent((Component)parent.getDateTextField());
			parent.setInvalid();
		}
	}
	
	/* (non-Javadoc)
	 * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getSuccessScript()
	 */
	@Override
	protected CharSequence getSuccessScript() {
		if (getEvent().equalsIgnoreCase(FOCUS_GAINED) && parent.isSelectOnEnter()) {
			return "document.getElementById('" + ((Component)parent.getDateTextField()).getMarkupId()+"').select();";
		}
		return null;
	}
}
